package com.github.thinwonton.quartz.misfire;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 测试简单触发器
 * 
 * @author Administrator
 *
 */
public class SimpleTriggerTest {

	private static final Logger logger = LoggerFactory.getLogger(SimpleTriggerTest.class);

	public static void main(String[] args) throws SchedulerException {
		try {
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

			logger.info("Scheduler Name:" + scheduler.getSchedulerName());

			// 调度器开始工作
			scheduler.start();

			//创建一个job，并且赋予一些数据
			JobDetail job = JobBuilder.newJob(MyJob.class).usingJobData("datakey", "datakey value").build();

			// 建立触发器，启动作业，该作业每5分钟执行一次
			SimpleTrigger trigger = TriggerBuilder.newTrigger().startNow()
					.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever())
					.build();

			// job和trigger注册到scheduler
			scheduler.scheduleJob(job, trigger);

			// 休眠13秒，让作业执行3次->0,5,10
			Thread.sleep(13 * 1000);

			// 关闭调度器，此时调度器将会经过shutting down -> paused -> shutdown complete三种状态
			scheduler.shutdown();
		} catch (SchedulerException | InterruptedException e) {
			e.printStackTrace();
		}
	}
}
